package com.baoxin.chenxixc4coach;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.baoxin.chenxixc4coach.bean.AudioCourse;
import com.baoxin.chenxixc4coach.bean.Course;
import com.baoxin.chenxixc4coach.bean.CourseStudent;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.bean.Potential;
import com.baoxin.chenxixc4coach.bean.Schedule;
import com.baoxin.chenxixc4coach.bean.TestStudent;
import com.baoxin.chenxixc4coach.bean.TrainStudent;
import com.baoxin.chenxixc4coach.bean.UncheckStudent;
import com.baoxin.chenxixc4coach.bean.UserInfo;
import com.baoxin.chenxixc4coach.bean.Video;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.Msg;
import com.baoxin.chenxixc4coach.utils.MyLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyCallback implements Callback {
    String URL;
    String result;
    Handler handler;

    public MyCallback(String URL, Handler handler) {
        this.URL = URL;
        this.handler = handler;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        Message msg = Message.obtain();
        msg.what = Msg.ONFAILURE;
        Bundle bundle = new Bundle();
        bundle.putString("msg", e.toString());
        msg.setData(bundle);
        msg.obj = e.toString();//for rio
        handler.sendMessage(msg);
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {

        result = response.body().string();
        Gson gson = new GsonBuilder().serializeNulls().create();
        String code = "";
        String msg;
        Message message = Message.obtain();
        Bundle bundle = null;
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(result);
            code = jsonObject.getString("code");
            msg = jsonObject.getString("msg");
            bundle = new Bundle();
            bundle.putString("msg", msg);
            message.setData(bundle);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (URL.equals(Interfaces.URL_GETVIFICATION_CODE)) {//验证码
            MyLog.d("", result);
            if (code.equals("0")) {
                message.what = Msg.GET_VERIFICATION_SUCCEED;
                handler.sendMessage(message);
            } else {
                message.what = Msg.GET_VERIFICATION_FAILED;
                handler.sendMessage(message);
            }

        } else if (URL.equals(Interfaces.URL_REGISTER)) {
            MyLog.d("", result);
            if (code.equals("0")) {
                message.what = Msg.REGISTER_SUCCEED;
                handler.sendMessage(message);
            } else {
                message.what = Msg.REGISTER_FAILED;
                handler.sendMessage(message);
            }

        } else if (URL.equals(Interfaces.URL_LOGIN_IN)) {
            MyLog.d("RESULT", result);
            if (code.equals("0")) {
                message.what = Msg.LOGIN_SUCCEED;
                String token = null;
                try {
                    token = jsonObject.getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.saveToken(token);
                handler.sendMessage(message);
            } else {
                message.what = Msg.LOGIN_FAILED;
                handler.sendMessage(message);
            }

        } else if (URL.equals(Interfaces.URL_USER_INFO)) {//用户信息
            MyLog.d("RESULT", result);
            try {
                if (code.equals("0")) {
                    message.what = Msg.REQUEST_SUCCEED;
                    JSONObject jsonUser = jsonObject.getJSONObject("user");
                    UserInfo userInfo = gson.fromJson(jsonUser.toString(), UserInfo.class);
                    bundle.putParcelable("userInfo", userInfo);

                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_HEAD_NEWS)) {//驾考头条列表
            MyLog.d("", result);
            if (code.equals("0")) {
                try {
                    JSONArray newsJson = jsonObject.getJSONArray("dataList");
                    ArrayList<News> newsList = gson.fromJson(newsJson.toString(), new TypeToken<List<News>>() {
                    }.getType());
                    message.obj = newsList;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                message.what = Msg.REQUEST_SUCCEED;
            } else {
                message.what = Msg.REQUEST_FAILED;
            }
            handler.sendMessage(message);
        } else if (URL.equals(Interfaces.URL_TEACH_VIDEOS)) {//教学视频
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray videoArray = jsonObject.getJSONArray("dataList");
                    ArrayList<Video> videoList = gson.fromJson(videoArray.toString(), new TypeToken<List<Video>>() {
                    }.getType());
                    message.obj = videoList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_GENJIN_LIST)) {//跟进学员列表
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray potentialArray = jsonObject.getJSONArray("list");
                    ArrayList<Potential> potentialList = gson.fromJson(potentialArray.toString(), new TypeToken<List<Potential>>() {
                    }.getType());
                    message.obj = potentialList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_POSTER_LIST)) {//海报列表
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray jsonArray = jsonObject.getJSONArray("urlList");
                    ArrayList<String> urls = gson.fromJson(jsonArray.toString(), new TypeToken<ArrayList<String>>() {
                    }.getType());
                    message.obj = urls;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_RELEASE_COURSES)) {//发布课程
            MyLog.d("", result);

            if (code.equals("0")) {
                message.what = Msg.REQUEST_SUCCEED;
            } else {
                message.what = Msg.REQUEST_FAILED;
            }
            handler.sendMessage(message);

        } else if (URL.equals(Interfaces.URL_GET_COURSE)) {//获取课程
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray courseArray = jsonObject.getJSONArray("list");
                    ArrayList<Course> courseList = gson.fromJson(courseArray.toString(), new TypeToken<List<Course>>() {
                    }.getType());
                    message.obj = courseList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_TRAINING_STUDENT)) {//在培学员
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray trainArray = jsonObject.getJSONArray("list");
                    ArrayList<UncheckStudent> trainList = gson.fromJson(trainArray.toString(), new TypeToken<List<TrainStudent>>() {
                    }.getType());
                    message.obj = trainList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_TEST_STUDENT)) {//考试学员
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray testArray = jsonObject.getJSONArray("list");
                    ArrayList<TestStudent> testList = gson.fromJson(testArray.toString(), new TypeToken<List<TestStudent>>() {
                    }.getType());
                    message.obj = testList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_UNCHECK_STUDENTS)) {//待审核关注的学员
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray uncheckArray = jsonObject.getJSONArray("list");
                    ArrayList<UncheckStudent> uncheckList = gson.fromJson(uncheckArray.toString(), new TypeToken<List<UncheckStudent>>() {
                    }.getType());
                    message.obj = uncheckList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_ARRANGE_TEST)) {//安排考试
            if (code.equals("0")) {
                message.what = Msg.REQUEST_SUCCEED;
            } else {
                message.what = Msg.REQUEST_FAILED;
            }
            handler.sendMessage(message);
        } else if (URL.equals(Interfaces.URL_COURSE_STUDENTS)) {//课程学员
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray array = jsonObject.getJSONArray("list");
                    ArrayList<CourseStudent> dataList = gson.fromJson(array.toString(), new TypeToken<List<CourseStudent>>() {
                    }.getType());
                    message.obj = dataList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_ADD_AGENDA)) {
            MyLog.d("", result);
            if (code.equals("0")) {
                message.what = Msg.REQUEST_SUCCEED;
            } else {
                message.what = Msg.REQUEST_FAILED;
            }
            handler.sendMessage(message);
        } else if (URL.equals(Interfaces.URL_AGENDA_LIST)) {
            MyLog.d("", result);
            try {
                if (code.equals("0")) {
                    JSONArray scheduleArray = jsonObject.getJSONArray("list");
                    ArrayList<Schedule> scheduleList = gson.fromJson(scheduleArray.toString(), new TypeToken<List<Schedule>>() {
                    }.getType());
                    message.obj = scheduleList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else if (URL.equals(Interfaces.URL_ADD_AUDIO)) {
            MyLog.d("", result);
            if (code.equals("0")) {
                message.what = Msg.REQUEST_SUCCEED;
            } else {
                message.what = Msg.REQUEST_FAILED;
            }
            handler.sendMessage(message);
        } else if (URL.equals(Interfaces.URL_LIST_AUDIO)) {
            MyLog.d("aaa", result);
            try {
                if (code.equals("0")) {
                    JSONArray audioArray = jsonObject.getJSONArray("list");
                    ArrayList<AudioCourse> audioList = gson.fromJson(audioArray.toString(), new TypeToken<List<AudioCourse>>() {
                    }.getType());
                    message.obj = audioList;
                    message.what = Msg.REQUEST_SUCCEED;
                } else {
                    message.what = Msg.REQUEST_FAILED;
                }
                handler.sendMessage(message);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
